﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace CompleteReader.DocumentsPageDialogs
{
    public sealed partial class PasswordDialog : UserControl
    {
        public delegate void OnPasswordEntered(string password);

        /// <summary>
        ///  Will provide a password string when the user clicks ok. If the user selects cancel, it will give a null value
        /// </summary>
        public event OnPasswordEntered PasswordEntered;


        public PasswordDialog()
        {
            this.InitializeComponent();
            OuterGrid.Height = Window.Current.Bounds.Height - 75;
        }

        public void SetFocus()
        {
            this.PasswordEntry.Focus(FocusState.Programmatic);
        }

        public void Reset()
        {
            this.PasswordEntry.Password = "";
        }

        public void NotifyIncorrectPassword()
        {
            IncorrectPasswordNotification.Visibility = Windows.UI.Xaml.Visibility.Visible;
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            if (PasswordEntered != null)
            {
                PasswordEntered(PasswordEntry.Password);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (PasswordEntered != null)
            {
                PasswordEntered(null);
                com.IronOne.BoardPACWinApp.View.Page.AgendaPage.GlobalSelectdAgendaItem = null;
            }
        }

        private void PasswordEntry_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter && !string.IsNullOrEmpty(PasswordEntry.Password))
            {
                if (PasswordEntered != null)
                {
                    PasswordEntered(PasswordEntry.Password);
                }
                e.Handled = true;
            }
        }

        private void PasswordEntry_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(PasswordEntry.Password))
            {
                OkButton.IsEnabled = false;
            }
            else
            {
                OkButton.IsEnabled = true;
            }
            IncorrectPasswordNotification.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
    }
}
